﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using ChinaSoft.BLL;
using ChinaSoft.Model;

namespace Admin.Sys.Role
{
    public partial class RoleToUser_Edit : System.Web.UI.Page
    {
        private BLL_T_SYS_ROLEUSER bllRoleUser = new BLL_T_SYS_ROLEUSER();
        private Model_T_SYS_ROLEUSER modelRoleUser = new Model_T_SYS_ROLEUSER();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
                ControlBtn(false, false, false, false, false);
                BindUser();
            }
        }
        /// <summary>
        /// 控制控件的状态（禁用、启用）
        /// </summary>
        /// <param name="btnAdd"></param>
        /// <param name="btnDel"></param>
        /// <param name="btnUpdate"></param>
        /// <param name="btnSave"></param>
        /// <param name="btnCancel"></param>
        private void ControlBtn(bool btnAdd, bool btnDel, bool btnUpdate, bool btnSave, bool btnCancel)
        {
            imgBtnAdd.Enabled = btnAdd;
            imgBtnDel.Enabled = btnDel;
            imgBtnUpdate.Enabled = btnUpdate;
            imgBtnSave.Enabled = btnSave;
            imgBtnCancel.Enabled = btnCancel;
        }
        /// <summary>
        /// 绑定树性菜单
        /// </summary>
        private void BindTreeView()
        {
            TreeView1.Nodes.Clear();
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = bllBind.GetDataSet("sys_role_id,name", "t_sys_role", "");
            ChinaSoft.BLL.Common.TreeViewOperate bllTreeView = new ChinaSoft.BLL.Common.TreeViewOperate();
            bllTreeView.BindTreeView(ds, TreeView1, "NAME", "sys_role_id", false);
        }
        /// <summary>
        /// 绑定树形用户
        /// </summary>
        private void BindUser()
        {
            TreeView2.Nodes.Clear();
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = bllBind.GetDataSet("sys_user_id,name", "t_sys_user", "");
            ChinaSoft.BLL.Common.TreeViewOperate bllTreeView = new ChinaSoft.BLL.Common.TreeViewOperate();
            bllTreeView.BindTreeView(ds, TreeView2, "NAME", "sys_user_id", false);
        }
        /// <summary>
        /// 获取该角色下的用户，并勾选出来
        /// </summary>
        /// <param name="roleId"></param>
        private void GetRoleUserCheckedByRoleId(string roleId)
        {
            BLL_T_SYS_ROLEUSER bllRoleUser=new BLL_T_SYS_ROLEUSER();
            DataSet ds=bllRoleUser.GetList(" RoleId="+roleId);
            if (ds!=null&&ds.Tables.Count>0&&ds.Tables[0].Rows.Count>0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    for (int j = 0; j < TreeView2.Nodes.Count; j++)
                    {
                        if (ChinaSoft.Common.Input.SetString(ds.Tables[0].Rows[i]["UserID"]) == TreeView2.Nodes[j].Value)
                            TreeView2.Nodes[j].Checked = true;
                        else
                            TreeView2.Nodes[j].Checked = false;
                    }
                }
            }
        }
        /// <summary>
        /// 当树形菜单选择改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            string roleId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            HiddenRoleId.Value = roleId;
            GetRoleUserCheckedByRoleId(roleId);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ClearTreeViewChecked(TreeView2);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            string roleId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            bool result = bllBind.DeleteWhere("t_sys_roleuser", " RoleID=" + roleId);
            if (result)
            {
                ChinaSoft.Common.MessageBox.Show(this, "删除成功！");
                BindTreeView();
            }
            else
            {
                ChinaSoft.Common.MessageBox.Show(this, "该数据已经不存在");
                BindTreeView();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ClearTreeViewChecked(TreeView2);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
           //删除该角色下所有用户，然后添加新的勾选的用户
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            bllBind.DeleteWhere("t_sys_roleuser", "RoleID=" + HiddenRoleId.Value);

            for (int i = 0; i < TreeView2.Nodes.Count; i++)
            {
                if (TreeView2.Nodes[i].Checked)
                {
                    modelRoleUser.ROLEID = HiddenRoleId.Value;
                    modelRoleUser.USERID = TreeView2.Nodes[i].Value;
                    bllRoleUser.Add(modelRoleUser);
                }
            }
            ChinaSoft.Common.MessageBox.Show(this, "保存成功！");
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnCancel_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            ClearTreeViewChecked(TreeView2);
        }

        private void ClearTreeViewChecked(TreeView treeview)
        {
            for (int i = 0; i < treeview.Nodes.Count; i++)
            {
                treeview.Nodes[i].Checked = false;
            }
        }
    }
}